<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="@/assets/logo.png" alt />
      </div>
      <!--/ 头像区域 -->

      <!-- 登录区域 -->
      <!-- 给form添加双向数据绑定 -->
      <!-- :冒号用于更新属性 -->
      <!-- 绑定校验属性rules -->
      <!-- 通过ref获取表单的实例对象 -->
      <el-form
        ref="LoginFormRef"
        label-width="0px"
        class="login_form"
        :model="LoginForm"
        :rules="LoginFormRules"
      >
        <!-- 绑定需要校验的prop -->
        <el-form-item prop="username">
          <!-- 引入阿里字体图标 -->
          <el-input prefix-icon="iconfont icon-user" v-model="LoginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            prefix-icon="iconfont icon-3702mima"
            v-model="LoginForm.password"
          ></el-input>
        </el-form-item>
        <!-- /登录区域 -->

        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <!-- 添加登录验证事件 -->
          <el-button type="primary" @click="login">登录</el-button>
          <!-- 添加重置表单事件 -->
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
        <!-- 按钮区域 -->
      </el-form>
    </div>
  </div>
</template>


<script>
export default {
  data() {
    return {
      // 这是登陆表单的数据绑定对象
      LoginForm: {
        username: "admin",
        password: "123456",
      },
      LoginFormRules: {
        // 设定校验规则,规则条件需要在数组中用对象形式存储
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 重置按钮  重置表单
    resetLoginForm() {
      this.$refs.LoginFormRef.resetFields();
    },
    login() {
      this.$refs.LoginFormRef.validate(async (value) => {
        if (!value) return;
        // console.log(this);
        const { data: res } = await this.$axios.post("login", this.LoginForm);
        if (res.meta.status !== 200) return this.$message.error('登录错误') 
        this.$message({
          message: "登陆成功",
          type: "success",
        });
        // 将token保存到客户端的sessionStorage中
        // 通过编程式导航跳转到后台主页，路由地址是/home
        console.log(res);
        window.sessionStorage.setItem("token",res.data.token)
        this.$router.push('/home')


      });
    },
  },
};
</script>

<style lang="less" scoped>
.login-container {
  height: 100%;
  background-color: #2b4b6b;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 3px;
}
.avatar_box {
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 130px;
  height: 130px;
  border: 1px solid #eee;
  padding: 10px;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  background-color: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 10px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
